package com.xcity.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xcity.config.ConfigEx;
import com.xcity.db.entity.Build;
import com.xcity.db.entity.Employee;
import com.xcity.db.entity.GuildMember;
import com.xcity.db.entity.Mail;
import com.xcity.db.entity.Player;
import com.xcity.game.activity.cumulativerecharge.CumulativeRechargeActivityService;
import com.xcity.game.activity.luckdraw.BigLuckDrawActivityService;
import com.xcity.game.activity.mooncard.MoonCardActivityService;
import com.xcity.game.activity.sevendays.SevenDaysSignInActivityService;
import com.xcity.game.activity.str.StrengthMeal;
import com.xcity.game.activity.str.StrengthMealActivityService;
import com.xcity.game.activity.thirtyday.ThirtyDaySignActivityService;
import com.xcity.game.activity2.ActivityService2;
import com.xcity.game.activity2.rechargereward.RechargeRewardActivity;
import com.xcity.game.battle.BattleResult.PVEResult;
import com.xcity.game.battle.pve.PVEStage;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2.State;
import com.xcity.game.buildv2.template.BuildTemplateV2;
import com.xcity.game.chat.ChatService;
import com.xcity.game.combat.CombatException;
import com.xcity.game.common.Cause;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.common.GameStaticConfig.SectionProb;
import com.xcity.game.common.LogCause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.employee.EmployeeException;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.config.EmployeeServiceConfigure;
import com.xcity.game.employee.template.EmployeeTemplate;
import com.xcity.game.event.GameEvents;
import com.xcity.game.formation.Formation;
import com.xcity.game.guild.GuildService;
import com.xcity.game.item.GameItem;
import com.xcity.game.land.LandService;
import com.xcity.game.mail.MailAttach;
import com.xcity.game.mail.MailService;
import com.xcity.game.map.MapService;
import com.xcity.game.peakroad.model.PeakRoadInstance;
import com.xcity.game.pet.Pet;
import com.xcity.game.player.PlayerService;
import com.xcity.game.recharge.RechargeTemplate;
import com.xcity.game.shop.mystery.MysteryShopService;
import com.xcity.game.task.TaskService;
import com.xcity.game.treasurebox.TreasureBoxState;
import com.xcity.game.vip.VipTemplate;
import com.xcity.game.vip.VipUtils;
import com.xcity.game.worldevent.WorldEvent;
import com.xcity.game.worldevent.WorldEventService;
import com.xcity.pomelo.push.SyncPush;
import com.xcity.pomelo.push.msg.MysteryShopStatePush;
import com.xcity.pomelo.push.msg.TipPush;
import com.xcity.pomelo.push.msg.TreasureBoxPush;
import com.xcity.pomelo.push.sync.CustomSync;
import com.xcity.pomelo.push.sync.IntSync;
import com.xcity.pomelo.push.sync.StringSync;
import com.xcity.pomelo.request.ActivityBigLuckDrawRequest;
import com.xcity.pomelo.response.BuildListStatusResponse.BuildStatusRes;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.Response;

import gnu.trove.list.TLongList;
import gnu.trove.list.array.TLongArrayList;
import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.game.time.DayListener;
import naga.x.game.time.TimeUpdater;
import naga.x.util.NumberUtils;
import naga.x.util.StringUtils;
import naga.x.util.TimeUtils;

public class BackDoorHandler {
	
//	public static final boolean CHEAT_ENABLE = App.getApp().getConfig().getBooleanValue("cheat"); // 正式发布需改为false
	public static final boolean CHEAT_ENABLE = ((ConfigEx) App.getApp().getConfig()).cheat;
	static final String SESSION_CHEAT_ENABLE = ".cheat";
	static final String CHEAT_CODE = "/whosyourdaddy";
	
	static final String CMD_ADD_GOLD = "/addgold";
	static final String CMD_ADD_STONE = "/addstone";
	static final String CMD_ADD_EXP = "/addexp";
	static final String CMD_ADD_ITEM = "/additem";
	@Deprecated
	static final String CMD_ADD_BUFF = "/addbuff";
	static final String CMD_ADD_STR = "/addstr"; // 加体力
	static final String CMD_CLEAR_BAG = "/clearbag";
	@Deprecated
	static final String CMD_TRIGGER_WORLD_EVENT = "/worldevent";
	static final String CMD_DROP_TREASURE_BOX = "/droptreasure"; // 强制掉落一个宝箱
	static final String CMD_SEND_MAIL = "/sendmail"; // /sendmail 角色id->标题->内容->现金->钻石->物品id=物品数量
	static final String CMD_CLEAR_TREASURE_BOX = "/clearbx"; // 清除宝箱cd
	static final String CMD_SET_TREASURE_BOX_RATE = "/bxrate"; // 设置宝箱掉落概率
	static final String CMD_SERVER_TIME = "/time"; // 设置或获取(无参)服务器时间 /time yyyy-MM-dd HH:mm:ss
	static final String CMD_CLEAR_DAY_LOGIN = "/cleardaylogin"; // 清除每日登录
	@Deprecated
	static final String CMD_OPEN_MYSTERY_SHOP = "/openmshop"; // 开启神秘商店
	@Deprecated
	static final String CMD_CLOSE_MYSTERY_SHOP = "/closemshop"; // 关闭神秘商店
	@Deprecated
	static final String CMD_REFRESH_MYSTERY_SHOP = "/refreshmshop"; // 刷新神秘商店物品
	static final String CMD_CLEAR_7DAY = "/clear7day"; // 清除7日签到数据
	static final String CMD_7DAY = "/7day"; // 设置7日签到(/7day x: 设置当前为7日签到的第x天)
	@Deprecated
	static final String CMD_ADD_PET = "/addpet"; // 添加宠物
	@Deprecated
	static final String CMD_CHANGE_PET_SPIRIT = "/petspirit"; // /petspirit +n or /petspirit -n
	@Deprecated
	static final String CMD_PET_LEVEL = "/petlvl"; // 设置宠物等级 /petlvl petlvl
	@Deprecated
	static final String CMD_BUILD_LEVEL = "/buildlvl"; // 设置建筑等级 /buildlvl 建筑id 建筑等级
	static final String CMD_RESET_TAP_LIKE = "/resettaplike"; // 重置点赞
	static final String CMD_RESET_PVE = "/resetpve"; // 重置所有pve关卡次数
	static final String CMD_PASS_PVE = "/pvepass"; // 通过所有pve关卡
	static final String CMD_TASK_FINISH = "/taskfinish"; // 完成任务
	@Deprecated
	static final String CMD_BUILD_MONEY = "/buildmoney"; // 增减建筑收益
	static final String CMD_VIP_SET = "/vip"; // 设置vip等级
	static final String CMD_GUIDE = "/guide"; // 设置新手引导
	@Deprecated
	static final String CMD_BUILD_DELETE = "/builddel"; // 删除建筑
	static final String CMD_BUILD_CREAT_ALL = "/build";
//	static final String CMD_ADD_EMPLOYEE_SKILL = "/addeskill"; // 获得员工技能
	static final String CMD_BIG_LUCK_DRAW_POINTS = "/bigluckdrawpoints"; // 增加/减少大转盘积分
	@Deprecated
	static final String CMD_BIG_LUCK_DRAW = "/bigluckdraw"; // 大转盘抽奖
	static final String CMD_SYS_CHAT = "/syschat"; // 发送系统聊天
	static final String CMD_FORMATION_CHANGE = "/formation"; // 改变战斗阵型
	static final String CMD_ADD_EMPLOYEE = "/addemployee"; // 加员工
	static final String CMD_DELETE_EMPLOYEE = "/delemployee"; // 删除员工
	static final String CMD_ADD_SHIELD = "/addshield"; // 加/减护盾
	static final String CMD_MOON_CARD = "/mooncard"; //月卡活动
	static final String CMD_STRENGTH_MEAL = "/meal"; // 开启送体力活动（午餐、晚餐）
	@Deprecated
	static final String CMD_CIRCLE = "/circle"; //欢乐转盘活动
	static final String CMD_ADD_STAGE_STARS = "/addstars"; // 加副本星数
	static final String CMD_RESET_STARBOX = "/starbox";//重置副本星数
	static final String CMD_RESET_RES = "/resetres";//重置资源副本次数和时间
	static final String CMD_ADD_VIGOUR = "/addvigour";//增加精力
	static final String CMD_ADD_PEAK_ROAD_TIMES = "/peakroad"; // 加爬塔次数
	static final String CMD_ADD_PEAK_ROAD_DAYS = "/prday";//重置巅峰
	static final String CMD_ADD_PEAK_COIN = "/addpeakcoin"; // 加巅峰币
	static final String CMD_30DAY = "/30day";
	static final String CMD_POOL_SET = "/pool"; // /pool key value
	static final String CMD_NEWPLAYER_CHEAT = "/np";
	static final String CMD_CUMULATIVE = "/cumulative";
	static final String CMD_MINE = "/mine";
	static final String CMD_TASK_RESET = "/task";
	static final String CMD_ADD_HONOR = "/addhonor"; // 加荣誉
	static final String CMD_DAY_CHANGED = "/daychg";
	static final String CMD_RECHARGE_REWARD = "/rr";//充值送蝙蝠侠
	static final String CMD_ASSIST = "/ass";//协助防御
	static final String CMD_WORLD_BOSS_ADD_JOIN_TIMES = "/wb"; // 加世界boss挑战次数
	static final String CMD_GUILD = "/guild";//公会
	static final String CMD_OFFLINE_ARENA_ADD_JOIN_TIMES = "/addarena"; // 加竞技场挑战次数
	static final String CMD_ADD_SKILL_POINT = "/skp";//增加技能点
	
	static final List<String> CMDS = new ArrayList<String>(12);
	protected static final Logger LOG = LoggerFactory.getLogger(BackDoorHandler.class);
	
	static {
		Field[] fs = BackDoorHandler.class.getDeclaredFields();
		for (Field f : fs) {
			int mod = f.getModifiers();
			if ((mod & Modifier.FINAL) != 0 && (mod & Modifier.STATIC) != 0 && f.getType() == String.class) {
				try {
					String val = (String) f.get(null);
					if (val.charAt(0) == '/') {
						CMDS.add(val);
					}
				} catch (Exception e) {
				}
			}
		}
	}
	
	static boolean isCheatCmd(String cmd) {
		List<String> list = CMDS;
		for (String s : list) {
			if (cmd.startsWith(s)) {
				return true;
			}
		}
		return false;
	}

	public static boolean handle(Player player, int serial, String cmd) {
		if (!CHEAT_ENABLE && isCheatCmd(cmd)) {
			// 作弊没有开启的情况下输入命令则忽略
			return true;
		}
		if (!CHEAT_ENABLE || cmd.charAt(0) != '/') {
			return false;
		}
		Boolean enable = (Boolean) player.getSession().getAttribute(SESSION_CHEAT_ENABLE);
		if (CHEAT_CODE.equals(cmd)) {
			enable = true;
			player.getSession().setAttribute(SESSION_CHEAT_ENABLE, enable);
			player.send(new Response(serial));
			return true;
		}
		if (enable == null || !enable) {
			return false;
		}
		String[] cmds = StringUtils.split(cmd, "\\s+", true);
		if (CMD_ADD_GOLD.equals(cmds[0])) {
			if (cmds.length == 2) {
				long n = NumberUtils.parseLong(cmds[1]);
				boolean succ = n > 0 ? player.addGold(n, Cause.CHEAT) : player.costGold(-n, Cause.CHEAT);
			}
		} else if (CMD_ADD_STONE.equals(cmds[0])) {
			if (cmds.length == 2) {
				int n = NumberUtils.parseInt(cmds[1]);
				boolean succ = n > 0 ? player.addStone(n, Cause.CHEAT) : player.costStone(-n, Cause.CHEAT);
			}
		} else if (CMD_ADD_EXP.equals(cmds[0])) {
			if (cmds.length == 2) {
				player.addExp(NumberUtils.parseInt(cmds[1]), Cause.CHEAT);
			}
		} else if (CMD_ADD_ITEM.equals(cmds[0])) {
			if (cmds.length == 3) {
				int id = NumberUtils.parseInt(cmds[1]);
				int count = NumberUtils.parseInt(cmds[2]);
				player.getBag().addItemWithoutException(id, count, Cause.CHEAT);
			}
		} else if (CMD_CLEAR_BAG.equals(cmds[0])) {
			List<GameItem> list = player.getBag().getItems();
			for (int i = list.size() - 1; i >= 0; i--) {
				GameItem item = list.get(i);
				player.getBag().removeItem(item.getId(), item.getCount(), Cause.CHEAT);
			}
		} else if (CMD_TRIGGER_WORLD_EVENT.equals(cmds[0])) {
			WorldEventService wes = App.getApp().getServiceManager().get(WorldEventService.class);
			WorldEvent event = wes != null ? wes.getCurrentEvent() : null;
			if (event != null && event.getEndTime() >= TimeUpdater.getInstance().now()) {
				player.send(new HintResponse(serial, "当前世界事件还未结束"));
				return true;
			}
			wes.triggerNextEvent();
		} else if (CMD_DROP_TREASURE_BOX.equals(cmds[0])) {
			// trigger drop
			player.poolSet(PlayerPool.PROPERTY_TREASURE_BOX_STATE, TreasureBoxState.INITED);
			player.poolSet(PlayerPool.PROPERTY_LAST_DROP_TREASURE_BOX_TIME, TimeUpdater.getInstance().now());
			player.send(TreasureBoxPush.DEFAULT);
		} else if (CMD_SEND_MAIL.equals(cmds[0])) {
//			/sendmail 角色id->标题->内容->现金->钻石->物品id=物品数量
			if (cmds.length == 2) {
				String[] strs = StringUtils.split(cmds[1], "->");
				if (strs.length != 6) {
					player.send(new HintResponse(serial, "参数错误"));
					return true;
				}
				long destId = NumberUtils.parseLong(strs[0]);
				String title = strs[1];
				String content = strs[2];
				int gold = NumberUtils.parseInt(strs[3]);
				int stone = NumberUtils.parseInt(strs[4]);
				String[] itemStrs = StringUtils.split(strs[5], "=");
				if (itemStrs.length != 2) {
					player.send(new HintResponse(serial, "参数错误"));
					return true;
				}
				int itemId = NumberUtils.parseInt(itemStrs[0]);
				int itemCount = NumberUtils.parseInt(itemStrs[1]);
				Mail mail = new Mail(Mail.TYPE_NORMAL, player.getId(), destId != 0 ? destId : player.getId(), title, content);
				MailAttach attach = new MailAttach();
				mail.setAttachment(attach);
				attach.setGold(gold);
				attach.setStone(stone);
				attach.addItem(itemId, itemCount);
				App.getApp().getServiceManager().get(MailService.class).sendMail(mail);
			}
		} else if (CMD_ADD_BUFF.equals(cmds[0])) {
			if (cmds.length == 2) {
				player.getBuffs().addBuff(NumberUtils.parseInt(cmds[1]));
			}
		} else if (CMD_ADD_STR.equals(cmds[0])) {
			if (cmds.length == 2) {
				int n = NumberUtils.parseInt(cmds[1]);
				boolean succ = n > 0 ? player.addStrength(n, Cause.CHEAT) : player.decStrength(-n, Cause.CHEAT);
			}
		} else if (CMD_CLEAR_TREASURE_BOX.equals(cmds[0])) {
			player.getPool().remove(PlayerPool.PROPERTY_TREASURE_BOX_STATE);
			player.getPool().remove(PlayerPool.PROPERTY_LAST_DROP_TREASURE_BOX_TIME);
		} else if (CMD_SET_TREASURE_BOX_RATE.equals(cmds[0])) {
			if (cmds.length == 3) {
				SectionProb[] settings = GameStaticConfig.treasureBox;
				int index = NumberUtils.parseInt(cmds[1]) - 1;
				if (index < 0) {
					index = 0;
				} else if (index >= settings.length) {
					index = settings.length - 1;
				}
				int rate = NumberUtils.parseInt(cmds[2]);
				if (rate < 0) {
					rate = 0;
				} else if (rate > 100) {
					rate = 100;
				}
				try {
					Class<SectionProb> clazz = SectionProb.class;
					Field f = clazz.getDeclaredField("probability");
					f.setAccessible(true);
					f.setInt(settings[index], rate * 100);
				} catch (Exception e) {
					player.send(new HintResponse(serial, "设置失败"));
					return true;
				}
			}
		} else if (CMD_SERVER_TIME.equals(cmds[0])) {
			if (cmds.length == 1) {
				player.send(new HintResponse(serial, "服务器当前时间是 " + TimeUtils.format(TimeUpdater.getInstance().now())));
				return true;
			}
			Date date = TimeUtils.parse(cmds[1] + " " + cmds[2]);
			if (date == null) {
				player.send(new HintResponse(serial, "时间格式错误，正确的格式应该是 /time yyyy-MM-dd HH:mm:ss"));
				return true;
			}
			TimeUpdater.getInstance().setDate(date);
			Collection<Player> players = App.getApp().getServiceManager().get(PlayerService.class).getOnlinePlayers();
			for (Player p : players) {
				p.getSession().setAttribute(SessionUtils.LAST_HEARTBEAT_TIME, TimeUpdater.getInstance().now());
			}
			App.getApp().getServiceManager().get(LandService.class).dayChanged();
		} else if (CMD_CLEAR_DAY_LOGIN.equals(cmds[0])) {
			player.getPool().remove(PlayerPool.PROPERTY_LAST_GET_DAILY_REWARD_DAY);
		} else if (CMD_OPEN_MYSTERY_SHOP.equals(cmds[0])) {
			MysteryShopService service = App.getApp().getServiceManager().get(MysteryShopService.class);
			reflectSetField(service, "opened", true);
			player.poolSet(PlayerPool.PROPERTY_BLACKSHOP_HIDE_TIME, TimeUpdater.getInstance().now() + TimeUnit.MINUTES.toMillis(10));
			player.send(MysteryShopStatePush.OPEN);
		} else if (CMD_CLOSE_MYSTERY_SHOP.equals(cmds[0])) {
			MysteryShopService service = App.getApp().getServiceManager().get(MysteryShopService.class);
			reflectSetField(service, "opened", false);
			player.poolRemove(PlayerPool.PROPERTY_BLACKSHOP_HIDE_TIME);
			player.send(MysteryShopStatePush.CLOSE);
		} else if (CMD_REFRESH_MYSTERY_SHOP.equals(cmds[0])) {
			player.getPool().remove(PlayerPool.PROPERTY_MYSTERY_SHOP_ITEMS);
		} else if (CMD_CLEAR_7DAY.equals(cmds[0])) {
			player.getPool().remove(PlayerPool.PROPERTY_SEVEN_DAYS_LOGIN_FIRST_DAY);
			App.getApp().getServiceManager().get(SevenDaysSignInActivityService.class).playerLogin(player);
		} else if (CMD_7DAY.equals(cmds[0])) {
			if (cmds.length > 1) {
				int day = NumberUtils.parseInt(cmds[1]);
				if (day < 1) day = 1;
				else if (day > 7) day = 7;
				int today = TimeUpdater.getInstance().today();
				player.getPool().put(PlayerPool.PROPERTY_SEVEN_DAYS_LOGIN_FIRST_DAY, today - day + 1);
			}
		} else if (CMD_ADD_PET.equals(cmds[0])) {
			if (cmds.length > 1) {
				try {
					player.getPets().addPet(NumberUtils.parseInt(cmds[1]), Cause.CHEAT);
				} catch (Exception e) {
					player.send(new HintResponse(serial, "宠物id错误"));
					return true;
				}
			}
		} else if (CMD_CHANGE_PET_SPIRIT.equals(cmds[0])) {
			if (cmds.length > 1) {
				Pet pet = player.getPets().getWorkingPet();
				if (pet == null) {
					player.send(new HintResponse(serial, "请先设置工作宠物"));
					return true;
				}
				int n = NumberUtils.parseInt(cmds[1]);
				if (n > 0) {
					pet.addSpirit(n, Cause.CHEAT);
				} else {
					pet.decSpirit(-n, Cause.CHEAT);
				}
			}
		} else if (CMD_PET_LEVEL.equals(cmds[0])) {
			if (cmds.length > 1) {
				Pet pet = player.getPets().getWorkingPet();
				if (pet == null) {
					player.send(new HintResponse(serial, "请先设置工作宠物"));
					return true;
				}
				int level = NumberUtils.parseInt(cmds[1]);
				pet.setLevel(level);
			}
		} else if (CMD_BUILD_LEVEL.equals(cmds[0])) {
//			if (cmds.length > 2) {
//				int buildId = NumberUtils.parseInt(cmds[1]);
//				int level = NumberUtils.parseInt(cmds[2]);
//				Build build = player.getBuildings().getBuild(buildId);
//				if (build != null && level > 0) {
//					build.setLevel(level);
//					if (build.getLevel() > Build.BREAK_LEVEL && build.getLastAddReserveTime() == 0) {
//						build.setLastAddReserveTime(TimeUpdater.getInstance().now());
//						build.setReserve(Build.MAX_RESERVE);
//					}
//					player.send(new CustomSync(SyncPush.SYNC_BUILD, new BuildStatusRes(player, build)));
//					LoggerFactory.getLogger(BackDoorHandler.class).info("[BUILD SET LVL]ID[{}]BUILD[{}]LVL[{}]CAUSE[{}]", player.getId(), buildId, level, Cause.CHEAT);
//				}
//			}
		} else if (CMD_RESET_TAP_LIKE.equals(cmds[0])) {
			player.poolRemove(PlayerPool.PROPERTY_LAST_TAP_LIKE_DAY);
			player.poolRemove(PlayerPool.PROPERTY_TAP_LIKE_NUM);
			player.poolRemove(PlayerPool.PROPERTY_TAP_LIKE_PLAYERS);
		} else if (CMD_RESET_PVE.equals(cmds[0])) {
			List<PVEStage> list = player.getPveStages().getStages();
			for (int i = list.size() - 1; i >= 0; i--) {
				PVEStage stage = list.get(i);
				stage.setLastFightDay(0);
			}
		} else if (CMD_PASS_PVE.equals(cmds[0])) {
			int star = NumberUtils.parseInt(cmds[1]);
			if (star > PVEResult.FAIL.ordinal() && star <= PVEResult.STAR3.ordinal()) {
				List<PVEStage> stages = player.getPveStages().getStages();
				for (int i = 0; i < stages.size(); i++) {
					PVEStage stage = stages.get(i);
					stage.setStar(star);
					stage.setPassTime(TimeUpdater.getInstance().now());
					App.getApp().getEventManager().addEvent(GameEvents.EVENT_PVE_SUCCESS, player, stage, star, Collections.emptyList(), true);
				}
			}
		} else if (CMD_TASK_FINISH.equals(cmds[0])) {
			byte type = NumberUtils.parseByte(cmds[1]);
			player.getTasks().finish(type, Cause.CHEAT);
		} else if (CMD_BUILD_MONEY.equals(cmds[0])) {
			int buildId = NumberUtils.parseInt(cmds[1]);
			Build build = player.getBuildings().getBuild(buildId);
			if (build != null) {
				int money = NumberUtils.parseInt(cmds[2]);
				build.setTempMoney(money);
				player.send(new CustomSync(SyncPush.SYNC_BUILD, new BuildStatusRes(player, build)));
			}
		} else if (CMD_VIP_SET.equals(cmds[0])) {
			int vipLvl = NumberUtils.parseInt(cmds[1]);
			if (vipLvl > 0 && vipLvl <= VipUtils.getMaxLevel()) {
				player.setVipLvl(vipLvl);
				player.send(new IntSync(SyncPush.SYNC_VIP, player.getVipLvl()));
				VipTemplate vip = VipUtils.getTemplate(vipLvl);
				if (StringUtils.isNotEmpty(vip.getHeadIcon())) {
					player.send(new StringSync(SyncPush.SYNC_ICON, vip.getHeadIcon()));
				}
				App.getApp().getEventManager().addEvent(GameEvents.EVENT_VIP_CHANGED, player, 0);
			}
		} else if (CMD_GUIDE.equals(cmds[0])) {
			int step = NumberUtils.parseInt(cmds[1]);
			if (step >= 0) {
				player.poolSet(PlayerPool.PROPERTY_GUIDE, step);
				if (step == 0) {
					player.poolSet(PlayerPool.PROPERTY_ADVENTURE_STEPS, 0);
				}
			}
		} else if (CMD_BUILD_DELETE.equals(cmds[0])) {
			List<Build> buildings = player.getBuildings().getBuildings();
			if (cmds.length > 1) {
				int buildId = NumberUtils.parseInt(cmds[1]);
				for (int i = 0; i < buildings.size(); i++) {
					if (buildings.get(i).getId() == buildId) {
						buildings.remove(i);
						LOG.info("[BUILD REMOVE]ID[{}]BUILD[{}]CAUSE[{}]", player.getId(), buildId, Cause.CHEAT);
						break;
					}
				}
			} else {
				buildings.clear();
				LOG.info("[BUILD REMOVEALL]ID[{}]CAUSE[{}]", player.getId(), Cause.CHEAT);
			}
		}/* else if (CMD_ADD_EMPLOYEE_SKILL.equals(cmds[0])) {
			long eid = NumberUtils.parseLong(cmds[1]);
			int skillId = NumberUtils.parseInt(cmds[2]);
			Employee e = player.getEmployees().get(eid);
			SkillTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).get(SkillTemplate.class, skillId);
			if (temp != null) {
				e.addSkill(new EmployeeSkill(temp));
			}
		}*/ else if (CMD_BIG_LUCK_DRAW_POINTS.equals(cmds[0])) {
			int n = NumberUtils.parseInt(cmds[1]);
			player.poolAdd(PlayerPool.PROPERTY_ACTIVITY_BIG_LUCK_DRAW_POINTS, n);
		} else if (CMD_BIG_LUCK_DRAW.equals(cmds[0])) {
			ActivityBigLuckDrawRequest req = new ActivityBigLuckDrawRequest();
			req.setSerial(serial);
			App.getApp().getServiceManager().get(BigLuckDrawActivityService.class).requestDraw(player.getSession(), req);
			return true;
		} else if (CMD_SYS_CHAT.equals(cmds[0])) {
			String msg = player.getName() + " 发了条系统消息试试";
			if (cmds.length > 1) {
				msg = cmds[1];
			}
			App.getApp().getServiceManager().get(ChatService.class).sendSystemChat(msg);
		} else if (CMD_FORMATION_CHANGE.equals(cmds[0])) {
			int pos = NumberUtils.parseInt(cmds[1]);
			long eid = NumberUtils.parseLong(cmds[2]);
			try {
				Employee e = eid != 0L ? player.getEmployees().get(eid) : null;
				player.getFormations().get(Formation.TYPE_NORMAL).replace(pos, e);
			} catch (CombatException e) {
				player.send(new TipPush(e.getMessage()));
			}
		} else if (CMD_ADD_EMPLOYEE.equals(cmds[0])) {
			int eid = NumberUtils.parseInt(cmds[1]);
//			int count = cmds.length > 2 ? NumberUtils.parseInt(cmds[2]) : 1;
			EmployeeTemplate template = App.getApp().getServiceManager().get(TemplateService.class).get(EmployeeTemplate.class, eid);
			if (template == null) {
				player.send(new TipPush(MessageConst.EMPLOYEE_NOT_EXIST));
			} else {
				EmployeeServiceConfigure builder = App.getApp().getServiceManager().get(EmployeeService.class).configure();
				Employee employee = new Employee(template, player.getEmployees(), builder);
				player.getEmployees().addEmployee(employee, true, Cause.CHEAT);
			}
		} else if (CMD_DELETE_EMPLOYEE.equals(cmds[0])) {
			try {
				if (cmds.length > 1) {
					long eid = NumberUtils.parseLong(cmds[1]);
					player.getEmployees().remove(eid);
				} else {
					Collection<Employee> list = player.getEmployees().getEmployees();
					TLongList ids = new TLongArrayList(list.size());
					for (Employee employee : list) {
//						player.getEmployees().remove(employee.getId());
						ids.add(employee.getId());
					}
					for (long id : ids.toArray()) {
						player.getEmployees().remove(id);
					}
				}
			} catch (EmployeeException e) {
				player.send(new HintResponse(serial, e.getMessage()));
				return true;
			}
		} else if (CMD_ADD_SHIELD.equals(cmds[0])) {
			int n = NumberUtils.parseInt(cmds[1]);
			if (n > 0) {
				player.addShield(n, LogCause.UNKNOWN);
			} else {
				player.decShield(-n, LogCause.UNKNOWN);
			}
		} else if(CMD_MOON_CARD.equals(cmds[0])){
			MoonCardActivityService mcas = App.getApp().getServiceManager().get(MoonCardActivityService.class);
			if(cmds[1].equals("pass0")){
				mcas.tryPassDay(player, 0);
			}else if(cmds[1].equals("pass1")){
				mcas.tryPassDay(player, 1);
			}else if(cmds[1].equals("reset")){
				mcas.resetMoonCard(player);
			}else if(cmds[1].equals("charge0")){
				RechargeTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).getTemplates(RechargeTemplate.class).get(30011);
				mcas.handleRecharge(player, temp.getPrice(), temp);
			}else if(cmds[1].equals("charge1")){
				RechargeTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).getTemplates(RechargeTemplate.class).get(30012);
				mcas.handleRecharge(player, temp.getPrice(), temp);
			}
		}else if (CMD_RECHARGE_REWARD.equals(cmds[0])){
			RechargeRewardActivity rra = App.getApp().getServiceManager().get(ActivityService2.class).findImpl(RechargeRewardActivity.class);
			if(rra != null){
				if(cmds[1].equals("30016")){
					RechargeTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).getTemplates(RechargeTemplate.class).get(30016);
					rra.handleRecharge(player, temp.getPrice(), temp);
				}else if(cmds[1].equals("30015")){
					RechargeTemplate temp = App.getApp().getServiceManager().get(TemplateService.class).getTemplates(RechargeTemplate.class).get(30015);
					rra.handleRecharge(player, temp.getPrice(), temp);
				}
			}
		} else if (CMD_STRENGTH_MEAL.equals(cmds[0])) {
			StrengthMeal meal = App.getApp().getServiceManager().get(StrengthMealActivityService.class).getLunch();
			if (cmds.length > 1 && "close".equals(cmds[1])) {
				meal.close();
			} else {
				meal.reset(TimeUpdater.getInstance().now() + 5000);
			}
		} else if(CMD_CIRCLE.equals(cmds[0])){
//			CircleActivityService cas = App.getApp().getServiceManager().get(CircleActivityService.class);
//			if(cmds[1].equals("on")){
//				CircleActivityService.active = true;
//				CircleActivityService.rankActive = true;
//				CircleActivityService.shopActive = true;
//			}else if(cmds[1].equals("off")){
//				CircleActivityService.active = false;
//				CircleActivityService.rankActive = false;
//				CircleActivityService.shopActive = false;
//			}else if(cmds[1].equals("on1")){
//				CircleActivityService.rankActive = true;
//			}else if(cmds[1].equals("off1")){
//				CircleActivityService.rankActive = false;
//			}else if(cmds[1].equals("on2")){
//				CircleActivityService.shopActive = true;
//			}else if(cmds[1].equals("off2")){
//				CircleActivityService.shopActive = false;
//				
//			}else if(cmds[1].equals("roll0")){
////				cas.tryRoll(player, CircleActivityService.REQUEST_TYPE_FREE);
//				cas.tryRoll(player, CircleActivityService.REQUEST_TYPE_ONE);
//			}else if(cmds[1].equals("roll1")){
//				cas.tryRoll(player, CircleActivityService.REQUEST_TYPE_TEN);
////			}else if(cmds[1].equals("roll2")){
//			}else if(cmds[1].equals("resetfree")){
//				player.resetCircleFreeCount("CHEAT");
//			}else if(cmds[1].equals("addscore")){
//				if(cmds.length > 2){
//					int add = NumberUtils.parseInt(cmds[2]);
//					player.addCircleScore(add, "CHEAT");
//				}
//			}else if(cmds[1].equals("buy")){
//				if(cmds.length > 2){
//					int idx = NumberUtils.parseInt(cmds[2]);
//					cas.tryBuy(player, idx);
//				}
//			}else if(cmds[1].equals("myscore")){
//				cas.getRank(player);
//			}else if(cmds[1].equals("clear")){
//				cas.dayChanged();
//			}
		} else if (CMD_ADD_STAGE_STARS.equals(cmds[0])) {
			player.addStageStars(NumberUtils.parseInt(cmds[1]), Cause.CHEAT);
		} else if(CMD_RESET_STARBOX.equals(cmds[0])){
			player.getPveStages().resetStarBox();
		} else if(CMD_RESET_RES.equals(cmds[0])){
			player.getResStages().getTypeInfo().resetCount();
		} else if(CMD_ADD_VIGOUR.equals(cmds[0])){
			player.addVigour(NumberUtils.parseInt(cmds[1]), Cause.CHEAT);
		} else if (CMD_ADD_PEAK_ROAD_TIMES.equals(cmds[0])) {
			int n = Math.abs(NumberUtils.parseInt(cmds[1]));
			PeakRoadInstance instance = player.getPeakRoadInstance();
			instance.setLastChallengeDay(TimeUpdater.getInstance().today() - 1);
			instance.setJoinedTimes(instance.getJoinedTimes() - n);
		} else if (CMD_ADD_PEAK_ROAD_DAYS.equals(cmds[0])){
			PeakRoadInstance instance = player.getPeakRoadInstance();
			instance.fireChallengeFailed(null);
		} else if (CMD_ADD_PEAK_COIN.equals(cmds[0])) {
			player.addPeakCoin(NumberUtils.parseInt(cmds[1]), Cause.CHEAT);
		} else if(CMD_30DAY.equals(cmds[0])){
			if(cmds[1].equals("recharge")){
				int money = NumberUtils.parseInt(cmds[2]);
				App.getApp().getServiceManager().get(ThirtyDaySignActivityService.class).handleRecharge(player, money);
			}else if(cmds[1].equals("acitve")){
				if(NumberUtils.parseInt(cmds[2]) == 1){
					App.getApp().getServiceManager().get(ThirtyDaySignActivityService.class).setActive(true);
					player.send(new TipPush("活动开放"));
				}else{
					App.getApp().getServiceManager().get(ThirtyDaySignActivityService.class).setActive(false);
					player.send(new TipPush("活动关闭"));
				}
			}else if(cmds[1].equals("setday")){
				int day = NumberUtils.parseInt(cmds[2]);
				player.getPool().put(PlayerPool.PROPERTY_THIRTY_DAY_TOTAL_DAY, day);
				player.getPool().put(PlayerPool.PROPERTY_THIRTY_DAY_LAST_SIGN_DAY, 0);
			}
		} else if (CMD_POOL_SET.equals(cmds[0])) {
			String key = cmds[1];
			if (cmds.length > 2) {
				String val = cmds[2];
				player.poolSet(key, val);
			} else {
				player.poolRemove(key);
			}
		} else if(CMD_NEWPLAYER_CHEAT.equals(cmds[0])){
			if(cmds[1].equals("createday")){
				player.getNewPlayerTasks().cheatResetCreatDay();
			}
		} else if(CMD_CUMULATIVE.equals(cmds[0])){
			if(cmds[1].equals("charge")){
				App.getApp().getServiceManager().get(CumulativeRechargeActivityService.class).handleRecharge(player, NumberUtils.parseInt(cmds[2]));
			}else if(cmds[1].equals("day")){
				App.getApp().getServiceManager().get(CumulativeRechargeActivityService.class).setBeginDay(cmds[2]);
			}else if(cmds[1].equals("new")){
				if(NumberUtils.parseInt(cmds[2]) == 1){
					CumulativeRechargeActivityService.newRoleActive = true;
				}else{
					CumulativeRechargeActivityService.newRoleActive = false;
				}
			}
		}else if(CMD_MINE.equals(cmds[0])){
			if(cmds[1].equals("test")){
			}else if(cmds[1].equals("new")){
				MapService mapService = App.getApp().getServiceManager().get(MapService.class);
				for(int i = 0; i < 100; i++){
					mapService.nextPosition();
				}
			}else{
				MapService mapService = App.getApp().getServiceManager().get(MapService.class);
				mapService.findMinesByMap(NumberUtils.parseInt(cmds[1]));
			}
		}else if(CMD_TASK_RESET.equals(cmds[0])){
			TaskService ts = App.getApp().getServiceManager().get(TaskService.class);
			if(cmds[1].equals("reset")){
				ts.dayChanged();
			}
		}else if(CMD_BUILD_CREAT_ALL.equals(cmds[0])){
			if(cmds[1].equals("all")){
				List<BuildV2> buildings = player.getBuildings2().getBuildings();
				int i = 0;
				for (; i < buildings.size(); i++) {
					BuildV2 b = buildings.get(i);
					if (b.getState() != State.BUILT && b.getState() != State.CAPTURED) {
						b.setState(State.BUILT);
					}
				}
				int total = App.getApp().getServiceManager().get(TemplateService.class).getTemplatesList(BuildTemplateV2.class).size();
				for (; i < total; i++) {
					BuildV2 b = player.getBuildings2().createNextBuilding();
					if (b != null) {
						b.setState(State.BUILT);
					}
				}
			}
		} else if (CMD_ADD_HONOR.equals(cmds[0])) {
			int n = NumberUtils.parseInt(cmds[1]);
			if (n > 0) {
				player.addHonor(n, Cause.CHEAT);
			} else {
				player.decHonor(n, Cause.CHEAT);
			}
		} else if (CMD_DAY_CHANGED.equals(cmds[0])) {
			try {
				Field f = TimeUpdater.class.getDeclaredField("dayListeners");
				f.setAccessible(true);
				List<DayListener> lsrs = (List<DayListener>) f.get(TimeUpdater.getInstance());
				for (DayListener lsr : lsrs) {
					lsr.dayChanged();
				}
				LOG.info("[DAYCHANGED]{}", Cause.CHEAT);
			} catch (Exception e) {
				LOG.error(e.getMessage(), e);
			}
		} else if(CMD_ASSIST.equals(cmds[0])){
			player.getBuildings2().resetAssist();
		} else if (CMD_WORLD_BOSS_ADD_JOIN_TIMES.equals(cmds[0])) {
			int n = NumberUtils.parseInt(cmds[1]);
			player.poolAdd(PlayerPool.PROPERTY_WORLD_BOSS_LEFT_TIMES, n);
		} else if(CMD_GUILD.equals(cmds[0])){
			if(cmds.length > 1){
				if(cmds[1].equals("rd")){
					GuildMember member = App.getApp().getServiceManager().get(GuildService.class).getGuildMember(player.getId());
					member.getMemData().resetDonate();
				}else if(cmds[1].equals("rg")){
					GuildMember member = App.getApp().getServiceManager().get(GuildService.class).getGuildMember(player.getId());
					member.setLastGetDay(0);
				}else if(cmds[1].equals("joincd")){
					int min = Integer.parseInt(cmds[2]);
					GuildService.JOIN_GUILD_CD_MILLISECOND = min * 60 * 1000;
				}
			}
		} else if (CMD_OFFLINE_ARENA_ADD_JOIN_TIMES.equals(cmds[0])) {
			int n = NumberUtils.parseInt(cmds[1]);
			player.poolAdd(PlayerPool.PROPERTY_ARENA_FREE_NUM, n);
		} else if (CMD_ADD_SKILL_POINT.equals(cmds[0])){
			int n = NumberUtils.parseInt(cmds[1]);
			if(n > 0){
				player.addSkillPoints(n, "GM");
			}
		}
		player.send(new Response(serial));
		return true;
	}
	
	static void reflectSetField(Object obj, String field, Object fieldVale) {
		try {
			Class<?> clazz = obj.getClass();
			Field f;
			try {
				f = clazz.getDeclaredField(field);
			} catch (Exception e) {
				f = clazz.getSuperclass().getDeclaredField(field);
			}
			f.setAccessible(true);
			f.set(obj, fieldVale);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
